package com.example.ceramics.database.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.LinearLayout;

import com.example.ceramics.R;
import com.example.ceramics.database.DatabaseContants;
import com.example.ceramics.database.DatabaseHelper;
import com.example.ceramics.entity.GoodsEntity;

import java.util.ArrayList;
import java.util.List;

public class GoodsDaoImpl implements IGoodsDao {

    private final DatabaseHelper databaseHelper;

    public GoodsDaoImpl(Context context) {
        databaseHelper = new DatabaseHelper(context);
    }

    @Override
    public int addGoods(GoodsEntity goods) {
        if (hasGoods(goods.getGoodsId())) {
            return -1;
        }
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "insert into " + DatabaseContants.TABLE_GOODS +
                "(goodsName,goodsPrice,goodsSignature,goodsType,goodsImg) values(?,?,?,?,?)";
        db.execSQL(sql, new Object[]{goods.getGoodsName(), goods.getGoodsPrice(),
                goods.getGoodsSignature(), goods.getGoodsType(), goods.getGoodsImg()});
        db.close();
        return 1;
    }

    @Override
    public int deleteGoods(int goodsId) {
        if (!hasGoods(goodsId)) {
            return -1;
        }
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "delete from " + DatabaseContants.TABLE_GOODS + " where goodsId=?";
        db.execSQL(sql, new Object[]{goodsId});
        db.close();
        return 1;
    }

    @Override
    public int updateGoods(GoodsEntity goods) {
        if (!hasGoods(goods.getGoodsId())) {
            return -1;
        }
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "update " + DatabaseContants.TABLE_GOODS +
                " set goodsName=?,goodsPrice=?,goodsSignature=?,goodsType=?,goodsImg=? where goodsId=?";
        db.execSQL(sql, new Object[]{goods.getGoodsName(), goods.getGoodsPrice(),
                goods.getGoodsSignature(), goods.getGoodsType(), goods.getGoodsImg()});
        db.close();
        return 1;
    }

    @Override
    public boolean hasGoods(int goodsId) {
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        String sql = "select goodsId from " + DatabaseContants.TABLE_GOODS + " where goodsId=?";
        Cursor cursor = db.rawQuery(sql, new String[]{"" + goodsId});
        if (cursor.moveToFirst()) {
            cursor.close();
            db.close();
            return true;
        }
        cursor.close();
        db.close();
        return false;
    }

    @Override
    public GoodsEntity getGoods(int goodsId) {
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        String sql = "select * from " + DatabaseContants.TABLE_GOODS + " where goodsId=?";
        Cursor cursor = db.rawQuery(sql, new String[]{"" + goodsId});
        GoodsEntity goods = null;
        if (cursor.moveToNext()) {
            goods = cursorToGoods(cursor);
        }
        cursor.close();
        db.close();
        return goods;
    }

    @Override
    public List<GoodsEntity> listAllGoods() {
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        String sql = "select * from " + DatabaseContants.TABLE_GOODS;
        Cursor cursor = db.rawQuery(sql, null);
        List<GoodsEntity> goodsList = new ArrayList<>();
        while (cursor.moveToNext()) {
            goodsList.add(cursorToGoods(cursor));
        }
        cursor.close();
        db.close();
        return goodsList;
    }

    @Override
    public List<GoodsEntity> listGoodsByType(String type) {
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        String sql = "select * from " + DatabaseContants.TABLE_GOODS + " where goodsType = ?";
        Cursor cursor = db.rawQuery(sql, new String[]{type});
        List<GoodsEntity> goodsList = new ArrayList<>();
        while (cursor.moveToNext()) {
            goodsList.add(cursorToGoods(cursor));
        }
        cursor.close();
        db.close();
        return goodsList;
    }

    private GoodsEntity cursorToGoods(Cursor cursor) {
        GoodsEntity goods;
        int goodsId = cursor.getInt(cursor.getColumnIndex("goodsId"));
        String goodsName = cursor.getString(cursor.getColumnIndex("goodsName"));
        double goodsPrice = cursor.getDouble(cursor.getColumnIndex("goodsPrice"));
        String goodsSignature = cursor.getString(cursor.getColumnIndex("goodsSignature"));
        String goodsType = cursor.getString(cursor.getColumnIndex("goodsType"));
        int goodsImg = cursor.getInt(cursor.getColumnIndex("goodsImg"));
        goods = new GoodsEntity(goodsId, goodsName, goodsPrice, goodsSignature, goodsType, goodsImg);
        return goods;
    }

    @Override
    public boolean isEmpty() {
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        String sql = "select * from "+DatabaseContants.TABLE_GOODS;
        Cursor cursor = db.rawQuery(sql, null);
        if (cursor.moveToFirst()) {
            cursor.close();
            db.close();
            return false;
        }
        cursor.close();
        db.close();
        return true;
    }

    @Override
    public List<GoodsEntity> listRandom(int num) {
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        String sql = "select * from " + DatabaseContants.TABLE_GOODS + " ORDER BY random() LIMIT ?";
        Cursor cursor = db.rawQuery(sql, new String[]{""+num});
        List<GoodsEntity> goodsList = new ArrayList<>();
        while (cursor.moveToNext()) {
            goodsList.add(cursorToGoods(cursor));
        }
        cursor.close();
        db.close();
        return goodsList;
    }

    @Override
    public List<GoodsEntity> searchGoods(String searchWord) {
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        String sql = "select * from " + DatabaseContants.TABLE_GOODS + " WHERE goodsName like ?";
        Cursor cursor = db.rawQuery(sql, new String[]{"%"+searchWord+"%"});
        List<GoodsEntity> goodsList = new ArrayList<>();
        while (cursor.moveToNext()) {
            goodsList.add(cursorToGoods(cursor));
        }
        cursor.close();
        db.close();
        return goodsList;
    }

    @Override
    public void addAllGoods() {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        db.beginTransaction();
//        db.execSQL("insert into "+DatabaseContants.TABLE_GOODS+"(goodsName,goodsPrice,goodsSignature,goodsType,goodsImg)" +
//                " values('小绵羊',50,'十二生肖·小绵羊','十二生肖系列',R.mipmap.image1)");
//        db.execSQL("insert into "+DatabaseContants.TABLE_GOODS+"(goodsName,goodsPrice,goodsSignature,goodsType,goodsImg)" +
//                " values('aaa',10,'十二生肖·小绵羊','十二生肖系列',R.mipmap.image2)");
//        db.execSQL("insert into "+DatabaseContants.TABLE_GOODS+"(goodsName,goodsPrice,goodsSignature,goodsType,goodsImg)" +
//                " values('bbb',20,'十二生肖·小绵羊','十二生肖系列',R.mipmap.image3)");
//        db.execSQL("insert into "+DatabaseContants.TABLE_GOODS+"(goodsName,goodsPrice,goodsSignature,goodsType,goodsImg)" +
//                " values('bbb',30,'十二生肖·小绵羊','十二生肖系列',R.mipmap.image1)");


        String sql = "insert into " + DatabaseContants.TABLE_GOODS +
                "(goodsName,goodsPrice,goodsSignature,goodsType,goodsImg) values(?,?,?,?,?)";

        db.execSQL(sql,new Object[]{"十二生肖",49,"十二生肖系列，购买后随机抽取一个","十二生肖系列", R.mipmap.zodiac});
        db.execSQL(sql,new Object[]{"阴阳五行",49,"五行系列，购买后随机抽取一个","阴阳五行系列", R.mipmap.five_elements});
//        db.execSQL(sql,new Object[]{"四季",49,"四季系列，购买后随机抽取一个","四季系列", R.mipmap.four_seasons});
        db.execSQL(sql,new Object[]{"百鸟朝凤",49,"百鸟朝凤系列，购买后随机抽取一个","百鸟朝凤系列", R.mipmap.birds});
        db.execSQL(sql,new Object[]{"十二生肖·月兔盘",49,"","其他周边", R.mipmap.rabbit1});
        db.execSQL(sql,new Object[]{"十二生肖·月兔盘",49,"","其他周边", R.mipmap.rabbit2});
        db.execSQL(sql,new Object[]{"十二生肖·月兔盘",49,"","其他周边", R.mipmap.rabbit3});
        db.execSQL(sql,new Object[]{"瓷儿",49,"","人物形象", R.mipmap.character});
        db.setTransactionSuccessful();

        db.endTransaction();
        db.close();
    }
//        if (hasGoods(goods.getGoodsId())) {
//        return -1;
//    }
//    SQLiteDatabase db = databaseHelper.getWritableDatabase();
//    String sql = "insert into " + DatabaseContants.TABLE_GOODS +
//            "(goodsName,goodsPrice,goodsSignature,goodsType,goodsImg) values(?,?,?,?,?)";
//        db.execSQL(sql, new Object[]{goods.getGoodsName(), goods.getGoodsPrice(),
//            goods.getGoodsSignature(), goods.getGoodsType(), goods.getGoodsImg()});
//        db.close();
//        return 1;
}
